package com.cityu.bac.rfidDriver;

public class Utils {
    /**
     * Convert bytes to a hexadecimal string
     * @param bytes Raw data
     * @return A converted hexadecimal string
     */
	public static String ByteToHex(byte[] bytes)
    {
	    if(bytes == null)
	        return "";
	    
    	String str = "";
    	for(int i = 0;i < bytes.length;i++){
    		str += String.format("%02X ", bytes[i]);
    	}
    	return str;
    }
	
	public static String ByteToHex(byte[] bytes,int start,int count)
    {
        String str = "";
        start = start > bytes.length ? bytes.length : start;
        count = count > (bytes.length - start) ? (bytes.length - start) : count;
        
        for(int i = start;i < start+count;i++){
            str += String.format("%02X ", bytes[i]);
        }
        return str;
    }
	
	/**
	 * Convert bytes to a ASCII string
	 * @param bytes Raw data
	 * @return A converted ASCII string
	 */
	public static String ByteToChar(byte[] bytes)
    {
    	String str = "";
    	for(int i = 0;i < bytes.length;i++){
    		char c = (char)bytes[i];
    		if(c >= 0 && c < 256)                                                                      
    			str += String.format("%c", c);
    	}
    	return str;
    }
	
	/**
	 * Compare A and B
	 * @param A bytes A
	 * @param B bytes B
	 * @return Is A equal B
	 */
	public static boolean equalbyteArr(byte[] A, byte[] B)
    {
	    if(A == null || B == null)
	        return false;
        if (A.length != B.length)
        {
            return false;
        }

        for (int i = 0; i < A.length; i++)
        {
            if (A[i] != B[i])
            {
                return false;
            }
        }

        return true;
    }
	
	public static boolean equalbyteArr(byte[] A, byte[] B, int count)
    {
	    if(A == null || B == null)
            return false;
	    
        if (A.length < count || B.length < count)
        {
            return false;
        }

        for (int i = 0; i < count; i++)
        {
            if (A[i] != B[i])
            {
                return false;
            }
        }

        return true;
    }
	
	public static int stringToByte(String in, byte[] b){   
        if (b.length < in.length() / 2) {   
            return -1;  
        }   
          
        int j=0;   
        StringBuffer buf = new StringBuffer(2);   
        for (int i=0;i < in.length();i+=2){
            buf.insert(0, in.charAt(i));
            buf.insert(1, in.charAt(i+1));   
            int t = Integer.parseInt(buf.toString(),16);   
            b[j] = (byte)t;   
            j++;   
            buf.delete(0,2);   
        }   
        return j;   
        }  
    
    public static byte[] stringToByte(String in){
        in = in.replace(" ", "");
        byte out[] = new byte[in.length()/2];
        stringToByte(in,out);
        return out;
    }
    
    public static byte[] binaryStringToBytePadLeft(String in){
        in = in.replace(" ", "");
        byte out[] = new byte[(in.length()-1)/8 + 1];
        int start = 0;
        for(int i = 0;i < out.length;i++){
            
            int end = 0;
            if(i == 0)
                end =start + in.length()%8;
            else
                end = start + 8;
            end = end > in.length() ? in.length() : end;
            
            int value = Integer.parseInt(in.substring(start, end),2);
            out[i] = (byte)value;
            //switch high and low bits
//            out[i] = (byte) ( ((out[i]&0x0F)<<4) | ((out[i]&0xF0)>>4) );
            start = end;
        }
        return out;
    }
}
